package com.thememaker.util.packager;

import com.thememake.common.base.Strings;
import com.thememaker.lockstyle.bean.CheckOut;
import com.thememaker.lockstyle.bean.Lockscreen;
import com.thememaker.lockstyle.bean.Var;
import com.thememaker.lockstyle.bean.command.VariableCommand;
import com.thememaker.lockstyle.bean.ui.BaseView;
import com.thememaker.lockstyle.bean.ui.Button;
import com.thememaker.lockstyle.bean.ui.Image;
import com.thememaker.lockstyle.bean.ui.Slider;
import com.thememaker.lockstyle.parsii.ExpressionUtils;

import java.util.Optional;
import java.util.function.Consumer;

public interface CodeCheck {

    public static final String Notification = "notification";
    public static final String TimeSrcCode = "time/time.png";
    public static final String Hour1SrcIdCode = "ifelse(#time_format,#hour24/10,#hour12/10)";
    public static final String Hour2SrcIdCode = "ifelse(#time_format,#hour24%10,#hour12%10)";
    public static final String Error_Code = "错误代码：";
    public static final String Error_Use_Visibility = "代码写死了Visibility=\"0\",控件永远不会显示，请删除对应代码！";
    public static final String Error_Use_Button_Align = "按钮控件不能使用align属性，请删除对应代码！";
    public static final String Error_Miss_Slider_Speed = "滑动控件回弹速度缺失，请修改代码！";
    public static final String Error_Slider_ReachedState = "OPPO锁屏中滑动控件触发状态应该使用 ReachedUpState ，请修改代码！";
    public static final String Error_Slider_ReachedUpState = "掌酷引擎中滑动控件触发状态应该使用 ReachedState ，请修改代码！";
    public static final String Error_Lockscreen_ScreenWidth = "锁屏屏幕宽度适配错误 ，请修改代码！";
    public static final String Error_Lockscreen_Vibrate = "锁屏需要默认去掉震动 ，请修改代码！";
    public static final String Error_Lockscreen_Notification_Code = "锁屏头部属性错误，只有微乐主题头部需要添加通知栏代码 ，请修改代码！";
    public static final String Error_Notification = "通知栏代码缺失，请添加代码！";
    public static final String Error_More_Notification = "通知栏代码多余，只有vivo的掌酷锁屏才需要添加此行代码，请删除代码！";
    public static final String Error_Lockscreen_Deficiency = "锁屏文件头部缺失，影响锁屏启动";

    public static final String Error_Var_Const = "初始化值时 必须添加 const=\"true/false\"，请修改代码";
    public static final String Error_Time_Hour12 = "时间12小时制未做适配，请修改代码";
    public static final String Error_rotation_center = "旋转动画未设置旋转中心";
    public static final String Error_Lockscreen_File_Damaged = "锁屏文件已算坏，请查看文件";


    default void checkVar(Var var, Consumer<CheckOut> callBack){
        if (Strings.isNullOrEmpty(var.getConstString()) && !Notification.equals(var.getName())){
            if(var.getVariableAnimation()==null){
                callBack.accept(new CheckOut(1,Error_Var_Const + Error_Code + var.toString()));
            }
        }
    }

    void checkLockscreen(Lockscreen lockscreen, Consumer<CheckOut> callBack);

    void checkImage(Image image, Consumer<CheckOut> callBack);

    void checkSlider(Slider slider, Consumer<CheckOut> callBack);

    default void checkSliderInitSpeed(Slider slider, Consumer<CheckOut> callBack){
        if (Strings.isNullOrEmpty(slider.getBounceAccelation()) || Strings.isNullOrEmpty(slider.getBounceInitSpeed())){
            callBack.accept(new CheckOut(1,Error_Miss_Slider_Speed + Error_Code + slider.toString()));
        }
    }

    default void checkButton(Button button, Consumer<CheckOut> callBack){
        if (!Strings.isNullOrEmpty(button.getAlign()) || !Strings.isNullOrEmpty(button.getAlignV())){
            callBack.accept(new CheckOut(1,Error_Use_Button_Align + Error_Code + button.toString()));
        }
        Optional.ofNullable(button.getTriggers())
                .flatMap(triggers -> Optional.ofNullable(triggers.getTriggerMap()))
                .ifPresent(triggers -> triggers.keySet().forEach(key0 -> {
                    Optional.ofNullable(triggers.get(key0).getCommandMap())
                            .ifPresent(commands -> {
                                commands.keySet().forEach(key1 -> {
                                    if (commands.get(key1) instanceof VariableCommand) {
                                        VariableCommand variableCommand = (VariableCommand) commands.get(key1);
                                        if (Strings.isNullOrEmpty(variableCommand.getConstString())){
                                            callBack.accept(new CheckOut(1,Error_Var_Const+Error_Code+variableCommand.toString()));
                                        }
                                    }
                                });
                            });
                }));
    }

    default void checkVisibility(BaseView baseView, Consumer<CheckOut> callBack){
        if ("0".equals(baseView.getVisibility()) && callBack != null){
            callBack.accept(new CheckOut(1,Error_Use_Visibility + Error_Code + baseView.toString()));
        }
    }

    default void showErrorNotification(Boolean canShowError,Consumer<CheckOut> callBack){
        if (canShowError){
            callBack.accept(new CheckOut(1,Error_Notification));
        }
    }


}
